package com.winning.cqs.pregnantbabycare_cqs_v1.volley;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.RequestQueue.RequestFilter;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.RetryPolicy;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.winning.cqs.pregnantbabycare_cqs_v1.PregnantBabyQsCareApplication;
import com.winning.cqs.pregnantbabycare_cqs_v1.config.CommonValue;

public class VolleyClient {
	// private Context context;
	private static int SOCKET_TIMEOUT = 6*10*100;
	public static RequestQueue mRequestQueue = Volley
			.newRequestQueue(PregnantBabyQsCareApplication.getInstance()
					.getApplicationContext());


	

	private VolleyClient() {

	}



	public void GetJsonObject(String url, final Map<String, String> params,
			Listener<JSONObject> listener, ErrorListener errListener) {
		url = getAbsoluteUrl(url);
		JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url,
				null, listener, errListener) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				// TODO Auto-generated method stub
				return params;
			}

			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				// TODO Auto-generated method stub
				return PregnantBabyQsCareApplication.getInstance()
						.getHeaderparams();
			}

			@Override
			public RetryPolicy getRetryPolicy() {
				// TODO Auto-generated method stub
				RetryPolicy retryPolicy = new DefaultRetryPolicy(
						SOCKET_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
						DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
				return retryPolicy;
			}

		};
		mRequestQueue.add(jr);
	}

	public void GetJsonArray(String url, final Map<String, String> params,
			Listener<JSONArray> listener, ErrorListener errListener) {
		url = getAbsoluteUrl(url);
		JsonArrayRequest jr = new JsonArrayRequest(url, listener, errListener) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				// TODO Auto-generated method stub
				return params;
			}

			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				// TODO Auto-generated method stub
				return PregnantBabyQsCareApplication.getInstance()
						.getHeaderparams();
			}

			@Override
			public RetryPolicy getRetryPolicy() {
				// TODO Auto-generated method stub
				RetryPolicy retryPolicy = new DefaultRetryPolicy(
						SOCKET_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
						DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
				return retryPolicy;
			}
		};
		mRequestQueue.add(jr);

	}

	public static void GetString(String url, Object tag,
			final Map<String, String> pams, Listener<String> listener,
			ErrorListener errListener) {
		url = getAbsoluteUrl(url);
		FakeX509TrustManager.allowAllSSL();
		StringPostRequest stringPostRequest = new StringPostRequest(url,
				listener, errListener, pams) {

			@Override
			public Map<String, String> getHeaders() throws AuthFailureError {
				// TODO Auto-generated method stub
				return PregnantBabyQsCareApplication.getInstance()
						.getHeaderparams();
			}

			@Override
			public RetryPolicy getRetryPolicy() {
				// TODO Auto-generated method stub
				RetryPolicy retryPolicy = new DefaultRetryPolicy(
						SOCKET_TIMEOUT, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
						DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
				return retryPolicy;
			}

		};
		// mRequestQueue.cancelAll();
		// volley.jar
		addRequest(stringPostRequest, tag);
	}

	public static void CancleAll(Object tag) {
		mRequestQueue.cancelAll(tag);
	}

	public void CancleSpecial(Request<?> request) {
		RequestFilter r = new RequestFilter() {

			@Override
			public boolean apply(Request<?> request) {
				// TODO Auto-generated method stub
				return false;
			}
		};

	}

	private static String getAbsoluteUrl(String relativeUrl) {

		return CommonValue.BASE_API + relativeUrl;
	}

	public static void addRequest(Request<?> request, Object tag) {
		if (tag != null) {
			request.setTag(tag);
		}
		mRequestQueue.add(request);
	}
	
	
	
	
	

}
